<?php
/*
 * emarket - a website to create your own online-shop.  
 * Copyright (C) 2009  Frédéric Delorme
 *
 * This file is part of emarket.
 *
 * emarket is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * emarket is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with emarket.  If not, see <http://www.gnu.org/licenses/>.
 *
 * To contact me: frederic.delorme@gmail.com
 * to see some information on te project : http://code.google.col/p/os-emarket/
 *
 */
global $framework;
require_once($framework."/persistence/datapersistence.php");

/**
 * Class to manage data into flat file
 * @author Fr�d�ric Delorme<frederic.delorme@gmail.com>
 * @version 1.0
 */
class DataFile{
	private static $_instance;
	/* Products */
	private $entities;
	private $page_size=4;
		
	public function __construct(){
		$dateformat = __param("system","dateformat");
		$files = opendir("data");
		while(($file=readdir($files))!==false){
			if( $file!=="." &&
				$file!==".." &&
				!is_dir($file) &&
				strpos(".dat",$file)>0){
				__debug("data: $file");
				$data = parse_ini_file("data/".$file,true);
				$pos = strpos($file,".");
				$entity=substr($file,0,$pos);
				__debug("parsed $entity");
				$this->entities[$entity]=$data;
			}
		}
		loadParamerters();
	}
	
	protected function loadParameters(){
		foreach($this->entities['parameter'] as $group=>$list){
			$count = 0;
			foreach($list as $key=>$value){
				// replate $DATE$ placeholder by system date formated 
				// following configuration date format.
				$this->entities['parameter'][$group][$key] = 
					str_replace("%DATE%",date( __param("system","dateformat") ),
						$this->entities['parameter'][$group][$key]);
				$count++;
				if($count>$this->page_size) break;
			}
		}
	}		
	/**
	 * return the instance for this Data class.
	 * @return Data singleton instance.
	 */
	public function getInstance(){
		if(empty(self::$_instance)){
			self::$_instance = new Data();
		}
		return self::$_instance;
	}
	/**
	 * return all entities for input type.
	 * @param $type string name of entity type to be retrieved.
	 * @return array following this description: array('id_value'=>array( 'id'=> id_value, ...),...)
	 */
	public function getEntities($type){
		return $this->entities[$type];
	}
	/**
	 * return one specific entity for input type and id.
	 * @param $type string name of entity type to be retrieved.
	 * @param $id string unique identifier for the entity type.
	 * @return array following this description: array('id_value'=>array( 'id'=> id_value, ...),...)
	 */
	public function getEntity($type,$id){
		return $this->entities[$type][$id];
	}
	
	/**
	 * 
	 * @param $where
	 * @param $type
	 * @param $order
	 * @param $limit
	 * @return unknown_type
	 */
	public function  queryEntities($type="", $where="", $order="",$limit=""){
		$list=array();
		$count=0;
		foreach($this->entities[$type] as $item){
			
			$flag=false;
			if($where!=""){
				foreach($where as $whereclause){
					switch ($whereclause['comparator']){
						case ">":
							$flag=($item[$whereclause['field']]>$whereclause['value']);
							break;
						case "<":
							$flag=($item[$whereclause['field']]>$whereclause['value']);
							break;
						case "=":
							$flag=($item[$whereclause['field']]==$whereclause['value']);
							break;
						case "<=":
							$flag=($item[$whereclause['field']]<=$whereclause['value']);
							break;
						case ">=":
							$flag=($item[$whereclause['field']]>=$whereclause['value']);
							break;
						case "!=":
							$flag=($item[$whereclause['field']]!=$whereclause['value']);
							break;
					}
				}
			}else{
				$flag=true;
			}
			if($flag){
				$list+=$item;

				if(!empty($limit['max']) && $count<$limit['max']) {
					$count++;
				}
			}
		}
		/*if($order!=0){
			$list = sort($list);
		}*/
		print_r($list);
		return $list;
	}
	/**
	 * set the entity with the input values.
	 * @param $type string name of the entity type.
	 * @param $entity array data for the entity with the mandatory 'id' field.
	 */
	public function setEntity($type,$entity){
		$this->entities[$type][$entity['id']] = $entity;
	}
	/**
	 * Return a "system" parameter corresponding to the Group/Key input.
	 * @param $group string name of the group of parameters.
	 * @param $key string name of the parameter.
	 * @return string value of the parameter.
	 */
	public function getParam($group,$key){
		return $this->entities['parameter'][$group][$key];
	}
	/**
	 * Return a "system" group of parameters corresponding to the Group input.
	 * @param $group string name of the group of parameters.
	 * @return array of string (key=>value format) for the group of parameter.
	 */
	public function getGroupParams($group){
		return $this->getEntitiy("parameter",$group);
	}
}
?>